﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace CommonTools.ConfigureTools
{
    public class LocalAppSettings
    {
        private XmlDocument xdoc;
        private XmlNode _usersettings;
        string configfile;
        string confgdir;
        public LocalAppSettings(string configureFileName,string configureDirectory,string rootName)
        {
            try
            {
                configfile = configureFileName;
                confgdir = configureDirectory;
                if (!Directory.Exists(confgdir))
                {
                    Directory.CreateDirectory(confgdir);
                }
                bool result = File.Exists(configfile);
                xdoc = new XmlDocument();
                if (result)
                {
                    xdoc.Load(configfile);
                }
                else
                {
                    xdoc.AppendChild(xdoc.CreateElement(rootName));
                    xdoc.Save(configfile);
                }
                _usersettings = xdoc.ChildNodes[0];
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void UpdateUserSetting(string nodeName,string key, string value)
        {
            try
            {
                bool containskey = false;
                XmlNode tmp = _getNodeByName(_usersettings, nodeName);
                if (tmp == null)
                {
                    XmlElement ele = xdoc.CreateElement(nodeName);
                    _usersettings.AppendChild(ele);
                    tmp = ele as XmlNode;
                }
                foreach (XmlNode node in tmp.ChildNodes)
                {
                    XmlElement ele = node as XmlElement;
                    if (ele.GetAttribute("key").Equals(key))
                    {
                        ele.SetAttribute("value", value);
                        containskey = true;
                        break;
                    }
                }
                if (!containskey)
                {
                    XmlElement ele = xdoc.CreateElement("SettingItem");
                    ele.SetAttribute("key", key);
                    ele.SetAttribute("value", value);
                    tmp.AppendChild(ele);
                }
                xdoc.Save(configfile);
            }
            catch (Exception)
            {

            }
        }

        public string GetUserSettingByKey(string nodeName,string key)
        {
            string res = "nokey";
            try
            {
                XmlNode tmp = _getNodeByName(_usersettings, nodeName);
                foreach (XmlNode node in tmp.ChildNodes)
                {
                    XmlElement ele = node as XmlElement;
                    if (ele.GetAttribute("key").Equals(key))
                    {
                        res = ele.GetAttribute("value");
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        public Dictionary<string, string> GetAllSettings(string nodeName)
        {
            Dictionary<string, string> res = new Dictionary<string, string>();
            try
            {
                XmlNode tmp = _getNodeByName(_usersettings,nodeName);
                foreach (XmlNode node in tmp.ChildNodes)
                {
                    XmlElement ele = node as XmlElement;
                    res.Add(ele.GetAttribute("key"), ele.GetAttribute("value"));
                }
            }
            catch (Exception)
            {

            }
            return res;
        }

        private XmlNode _getNodeByName(XmlNode root, string nodeName)
        {
            if (nodeName == null)
            {
                return root;
            }
            XmlNode resNode = null;
            foreach (XmlNode node in root)
            {
                if (node.Name.Equals(nodeName))
                {
                    resNode = node;
                    break;
                }
            }
            return resNode;
        }
    }
}
